package com.microclouddisks.controller;

import com.amazonaws.services.s3.AmazonS3;
import com.microclouddisks.entity.diskUser;
import com.microclouddisks.entity.folder;
import com.microclouddisks.functions.AWS_S3;
import com.microclouddisks.functions.sendMail;
import com.microclouddisks.service.loginService;
import com.microclouddisks.service.mainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class loginController {

    @Autowired
    private loginService loginservice;
    @Autowired
    private mainService mainservice;

    @RequestMapping(value = "/login")
    public String login(HttpSession session, HttpServletRequest request, Model model) {
        System.out.println("------> controller : login() ");
        String usermail = request.getParameter("usermail");
        String userpasword = request.getParameter("userpassword");
        System.out.println("------>user message : request : usermail =  " + usermail);
        if (usermail == null || userpasword == null) {
            System.out.println("------> user is not login ");
            return "login";
        } else {
            diskUser users = loginservice.loginCheck(usermail, userpasword);
            if (users == null) {
                System.out.println("------> 用户密码错误 ! ");
                return "login";
            } else {
                session.setAttribute("diskuser", users);
                model.addAttribute("diskuser", users);
                System.out.println("------> 登陆成功!   ");
                return "redirect:main";
            }

        }
    }

    @RequestMapping(value = "/register")
    public String register(HttpSession session, HttpServletRequest request) {
        System.out.println("-------> controller : register()");
        diskUser users = new diskUser();
        String usermail = request.getParameter("usermail");
        String userpassword = request.getParameter("userpassword");
        users.setUsermail(usermail);
        users.setUserpassword(userpassword);
        users.setUsername("MicroCloudDisksUser");
        int bools = loginservice.register(users);
        if (bools == 1) {
            System.out.println("------> 注册成功 !");
            folder folder = new folder();
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = formatter.format(date);
            folder.setFolderid(users.getUsermail() + "-" + time);
            folder.setFoldername("我的文件夹");
            folder.setSizes("-");
            folder.setNum(0);
            folder.setFiletype("folder");
            folder.setParent(usermail);
            folder.setFilepath(usermail + "/" + "我的文件夹");
            folder.setUpdatetime(time);
            folder.setUsermail(users.getUsermail());
            folder.setIcon("img/folder6.jpg");
            mainservice.creatFolder(folder);
            session.setAttribute("diskuser", users);

            //s3
            AWS_S3 aws_s3 = new AWS_S3();
            AmazonS3 s3 = aws_s3.CreateS3Client();
            aws_s3.CreateFolder("microclouds", usermail + "/我的文件夹", s3);

            return "redirect:main";
        } else {
            System.out.println("------> 注册失败 ! ");
            return "login";
        }
    }

    @RequestMapping(value = "/mailcheck")
    @ResponseBody
    public String mailcheck(String mail, Model model) {
        System.out.println("这里是邮箱验证!   " + mail);
        sendMail sendMail = new sendMail();
        int randNum = 1 + (int) (Math.random() * ((999999 - 1) + 1));
        String codes = String.valueOf(randNum);
        boolean bool = sendMail.sendmail(codes, mail);
        model.addAttribute("xx", codes);
        System.out.println("邮件已经成功发送!");
        return codes;
    }

    //    验证邮箱是否已经注册
    @RequestMapping(value = "/mailnumcheck")
    @ResponseBody
    public String mailnumcheck(String mail) {
        System.out.println("这里是查询邮箱是否已经注册!   " + mail);
        String bools = loginservice.queryMailById(mail);
        if (bools == "1")
            return "1";
        else
            return "0";
    }

}
